﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace HCDV
{
    public class webInfo
    {
        // <summary>
        /// 获取指定网页源码
        /// </summary>
        /// <param name="url">指定的网页</param>
        /// <returns>网页源码</returns>
        public string getHtml(string url)
        {
            string strHTML = "";
            WebClient myWebClient = new WebClient();
            Stream myStream = myWebClient.OpenRead(url);
            StreamReader sr = new StreamReader(myStream, Encoding.GetEncoding("gb2312"));
            strHTML = sr.ReadToEnd();
            myStream.Close();
            return strHTML;
        }

        // <summary>
        /// 获取IP归属地
        /// </summary>
        /// <param name="strIP">IP地址</param>
        /// <returns>IP归属地</returns>
        public string GetIPAdress(string strIP)
        {
            try
            {
                string s = getHtml("http://ip.ws.126.net/ipquery?ip=" + strIP);
                return s;
            }
            catch (Exception ee)
            {
                return "";
            }
        }
        ///// <summary>
        ///// 获取网站域名归属地
        ///// </summary>
        ///// <param name="strWWW">网站域名</param>
        ///// <returns>网站域名归属地</returns>
        //private string GetWWWIP(string strWWW)
        //{
        //    if (string.IsNullOrEmpty(strWWW))
        //        return null;
        //    string s = getHtml("http://www.ip138.com/ips1388.asp?ip=" + strWWW + "&action=2");
        //    int a = s.IndexOf("<h1><font color=\"blue\">") + "<h1><font color=\"blue\">".Length;
        //    int b = s.IndexOf("</font></h1></td>");
        //    return s.Substring(a, b - a).Replace(tbWWW.Text, "").Replace(" >> ", "").Replace(" ", "");
        //}
    }
}
